package com.sky.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.domain.Employee;
import com.sky.domain.User;
import com.sky.service.EmployeeService;
import com.sky.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 尹稳健~
 * @version 1.0
 */
@Controller
@Api(tags = "用户模块")
public class UserController {

    @GetMapping("/getUser")
    public User getUser(){
        return new User();
    }

    @Autowired
    private UserService userService;

    @Autowired
    private EmployeeService employeeService;

    // 跳转登陆页面
    @ApiOperation("跳转登录页面")
    @GetMapping({"/","/login"})
    public String toLogin(){
        return "login";
    }

    // 登录
    @ApiOperation("登录功能")
    @PostMapping("/login")
    public String login(@ApiParam("登录名") @RequestParam("loginname") String loginname,
                        @ApiParam("密码") @RequestParam("password") String password,
                        @ApiParam("权限") @RequestParam("tip") String tip, Model model,
                        HttpSession session){
        // 根据登录权限返回页面
        session.setAttribute("tip",tip);
        // 先判断是管理员登录还是员工登录
        // 1是管理员 2是员工
        if ("1".equals(tip)){
            User user = userService.login(loginname, password);
            if (user==null){
                session.setAttribute("msg","账号或密码错误,请重试!");
                return "redirect:/login";
            }
            session.setAttribute("loginUser",user);
            return "redirect:/main";
        }else{
            Employee employee = employeeService.login(loginname, password);
            if (employee==null){
                return "redirect:/login";
            }
            session.setAttribute("loginUser",employee);
            return "redirect:/main";
        }

    }


    // 退出登录 | 切换用户
    @ApiOperation("退出登录")
    @RequestMapping(value = "/loginout",method = RequestMethod.GET)
    public String loginout(){
        return "redirect:/login";
    }

    // 用户展示且用户信息
    @ApiOperation("跳转登录页面")
    @GetMapping("/user/toUpdate")
    public String toUpdate(HttpSession session,Model model){
        User user = (User)session.getAttribute("loginUser");
        model.addAttribute("loginUser",user);
        model.addAttribute("tip","1");
        return "user/update";
    }

    // 更新信息
    @ApiOperation("用户更新")
    @PostMapping("/user/update")
    public String update(User user,HttpSession session,Model model){
        User user1 = (User)session.getAttribute("loginUser");
        user1.setUsername(user.getUsername());
        user1.setLoginname(user.getLoginname());
        user1.setPassword(user.getPassword());
        userService.update(user);
        model.addAttribute("loginUser",user1);
        return "redirect:user/update";
    }

    // 展示数据 分页展示 模糊查询
    @ApiOperation("数据展示")
    @GetMapping("/user/list")
    public String list(Model model,String content,
                       @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum ){
        PageHelper.startPage(pageNum,5);
        List<User> userList = userService.userList(content);
        PageInfo<User> pageInfo = new PageInfo<User>(userList);
        model.addAttribute("pageInfo",pageInfo);
        return "user/list";
    }

    // 跳转添加页面
    @ApiOperation("跳转添加页面")
    @GetMapping("/user/add")
    public String toAddPage(Model model, Integer id){
        System.out.println(id);
        // 根据id查询用户信息
        if (id != null){
            User user = userService.findUserById(id);
            model.addAttribute("user",user);
            model.addAttribute("tip","true");
        }else{
            model.addAttribute("tip","false");
        }
        return "user/add";
    }

    // 添加用户
    @ApiOperation("添加用户")
    @PostMapping("/user/add")
    public String add(Model model,User user){
        if (user.getId() !=  null){
            userService.update(user);
        }else{
            userService.insert(user);
        }
        return "redirect:/user/list";
    }

    // 删除用户
    @ApiOperation("删除用户")
    @GetMapping("/user/delete")
    public String delete(Integer id){
        userService.deleteById(id);
        return "redirect:/user/list";
    }
}
